package com.netik.main;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import android.content.Context;
import android.os.Environment;
import android.util.Log;


public class Addtofav  {
	
	private String path="/";
	private boolean ret;
	public void append (String Enq_id,String Enq_name,Context con) {
		
		 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = null;
		
         try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
         Document doc = null;
         
         createfolder();
         
         
		try {
			File myfile = openfile();
			doc = docBuilder.parse (myfile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
		
         addFragment(doc,Enq_id,Enq_name);
         
	}
	
	
	public File openfile(){
		
		File dir = Environment.getExternalStorageDirectory();
	    File yourFile = new File(dir, "fav1.txt");
	    
		 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = null;
		try {
			docBuilder = docBuilderFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         Document doc = null;
         
		try {
			doc = docBuilder.parse (yourFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return yourFile;
	}
	
	
	
	
	public void createfolder(){
		
		File file = new File(Environment.getExternalStorageDirectory(), path);
	    if (!file.exists()) {
	    	System.out.println("Folder Created");
	        if (!file.mkdirs()) {
	        	System.out.println("does not exits123");
	            Log.e("TravellerLog :: ", "Problem creating Image folder");
	            ret = false;
	        }
	        else{
	        	System.out.println("present");
	        }
	    }
	    else{
	    	System.out.println("Folder Already exists");
	    }

	    
	    
	    File textFile = new File(file, "fav1.txt"); 
	    
	    FileWriter writer = null;
		
	    if(!textFile.exists())
	    {
	    try {
			
				writer = new FileWriter(textFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    BufferedWriter out = new BufferedWriter(writer);
	    try {
			out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><root></root>");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	    try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    
	    }
		
	}
	

	
	
	
	 public  void addFragment (Document doc,String Enq_id,String Enq_name ) {
	        
		 	Element person;
	        Element rootElement = doc.getDocumentElement();
	        
	        if(doc==null)
	        	System.out.println("null");
	        
	        NodeList result = (NodeList) rootElement.getChildNodes();
	        System.out.println(result.getLength());
	        
	        DocumentFragment fragment = doc.createDocumentFragment();
	        
	        person = makePersonNode(doc,Enq_id,Enq_name);
	        fragment.appendChild(person);
	        rootElement.appendChild(fragment);
	        
	        for(int i =0;i<result.getLength();i++)
            {
            		
            	System.out.println(result.item(i).getNodeName());
            }
	        
	        
	        result = (NodeList) rootElement.getChildNodes();
	        System.out.println(result.getLength());
	        
	        
	       
	       
	        try {
	        	
	        	File dir = Environment.getExternalStorageDirectory();
	    	    File yourFile = new File(dir, "fav1.txt");
	    	    
	        	 TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				Source source = new DOMSource(doc);
				Result output = new StreamResult(yourFile);
				transformer.transform(source,output);  

			} catch (TransformerConfigurationException e) {
				// TODO Auto-generated catch block
				System.out.println("here11werfw");
				
				e.printStackTrace();
			} catch (TransformerException e) {
				// TODO Auto-generated catch block
				System.out.println("here11");
				e.printStackTrace();
			}
	        
	        
	   /*     File outputFile = new  File("/sample.txt");

	        FileOutputStream fileOut = openFileOutput(outputFile, true);
	        OutputStreamWriter osw = new OutputStreamWriter(fileOut);
	        osw.writeBytes(data);
	        osw.flush();
*/
	        
	        

	 		System.out.println("Done");
	 		System.out.println("File updated!");   
	    }
	 
	 
	    private Element makePersonNode(Document doc,String enq_id,String enq_name) {
	        Element nameNode = doc.createElement("Enq_id");
	        Text nametextNode = doc.createTextNode(enq_id);
	        nameNode.appendChild(nametextNode);

	        Element ageNode = doc.createElement("Enq_name");
	        Text phonetextNode = doc.createTextNode(enq_name);
	        ageNode.appendChild(phonetextNode);
	        
	        Element personNode = doc.createElement("doc");
	        personNode.appendChild(nameNode);
	        personNode.appendChild(ageNode);
	        return(personNode);
	    }
	
	
}